//package study.springboot.mvc.support.exception;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.web.error.ErrorAttributeOptions;
//import org.springframework.boot.web.servlet.error.ErrorAttributes;
//import org.springframework.boot.web.servlet.error.ErrorController;
//import org.springframework.http.MediaType;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.context.request.ServletWebRequest;
//import org.springframework.web.servlet.ModelAndView;
//import study.eggs.result.Results;
//import study.eggs.utils.JsonUtils;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.Map;
//
///**
// * 1.注解 @ControllerAdvice 方式只能处理控制器抛出的异常，此时请求已经进入控制器中
// * 2.类 ErrorController 方式可以处理所有的异常，包括未进入控制器的错误，比如404,401等错误
// * 3.如果应用中两者共同存在，则 @ControllerAdvice 方式处理控制器抛出的异常，类 ErrorController 方式未进入控制器的异常
// * 4.@ControllerAdvice 方式可以定义多个拦截方法，拦截不同的异常类，并且可以获取抛出的异常信息，自由度更大
// */
//@Slf4j
//@RestController
//public class GlobalErrorController implements ErrorController {
//
//    private static final String PATH = "redirect:/error";
//
//    private static final String ERROR_PATH = "/error";
//
//    private static final String KEY_STATUS_CODE = "javax.web.error.status_code";
//
//    private static final String KEY_EXCEPTION = "javax.web.error.exception";
//
//    private static final String KEY_MESSAGE = "javax.web.error.message";
//
//    @Autowired
//    private ErrorAttributes errorAttributes;
//
//    @Override
//    public String getErrorPath() {
//        return ERROR_PATH;
//    }
//
//    @RequestMapping(value = ERROR_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
//    public Object error(HttpServletRequest request, HttpServletResponse response) {
//        //解析 Exception
//        Exception ex = (Exception) request.getAttribute(KEY_EXCEPTION);
//        if (ex != null) {
//            if (ex instanceof VException) {
//                //VException
//                VException vex = (VException) ex;
////                return Results.fail(vex);
//            } else {
//                //other Exception
//                return Results.fail();
//            }
//        } else {
//            ErrorAttributeOptions options = ErrorAttributeOptions.defaults();
//            ServletWebRequest webRequest = new ServletWebRequest(request);
//            Map<String, Object> errAttr = errorAttributes.getErrorAttributes(webRequest, options);
//            log.info(">>>>>> error info: {}", JsonUtils.toJson(errAttr));
//            return errAttr;
//        }
//    }
//
//    @RequestMapping(value = ERROR_PATH, produces = MediaType.TEXT_HTML_VALUE)
//    public ModelAndView errorPage(HttpServletRequest request) {
//        return new ModelAndView("globalError");
//    }
////
////    private Map<String, Object> getErrorAttributes(HttpServletRequest request) {
////        RequestAttributes requestAttributes = new ServletRequestAttributes(request);
////        Map<String, Object> map = errorAttributes.getErrorAttributes(requestAttributes);
////        if (request.getAttribute("status") instanceof Integer) {
////            map.put("status", request.getAttribute("status"));
////        }
////        map.put("url", request.getRequestURL().toString());
////        map.putIfAbsent("path", request.getAttribute("raw_url"));
////        return map;
////    }
//
//}
